<?php

namespace app\company\controller;

use app\admin\model\District;
use app\common\controller\Api;
use app\common\controller\CompanyApi;
use app\common\model\Area;
use think\Request;

/**
 * 地址接口
 */
class Notices extends CompanyApi
{
    protected $noNeedLogin = '';
    protected $noNeedRight = ['*'];

    protected $model = null;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $this->model = new \app\admin\model\Notices();
    }

    /**
     * 获取列表
     * @ApiSummary  (WanlShop 地址接口获取地址列表)
     * @ApiMethod   (GET)
     *
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        $type = $this->request->post('type', 'platform');

        $num1 = $this->model
            ->where('user_id', $this->auth->id)
            ->where('type', 'platform')
            ->where('is_read', 0)
            ->count();

        $num2 = $this->model
            ->where('user_id', $this->auth->id)
            ->where('type', 'profile')
            ->where('is_read', 0)
            ->count();

        $num3 = $this->model
            ->where('user_id', $this->auth->id)
            ->where('type', 'cost')
            ->where('is_read', 0)
            ->count();

        $list = $this->model
            ->field('id,type,is_read,content,createtime')
            ->where('user_id', $this->auth->id)
            ->where('type', $type)
            ->order('id desc')
            ->paginate()->each(function ($item) {
                $item['createtime'] = date('Y-m-d H:i', $item['createtime']);

                $types = [
                    'platform' => '平台通知',
                    'profile' => '盈利消息',
                    'cost' => '损耗消息',
                ];

                $item['type_name'] = $types[$item['type']];
            });

        $this->success('消息列表为', ['num1' => $num1, 'num2' => $num2, 'num3' => $num3, 'lists' => $list]);
    }

    /**
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function read()
    {
        $id = $this->request->post('id');
        if (!$id) {
            $this->error('参数缺失！');
        }

        $row = $this->model->find($id);
        if (!$row) {
            $this->error('该记录不存在！');
        }

        if ($row->is_read == 0) {
            $row->is_read = 1;
            $row->save();
        }

        $this->success('内容为', $row);
    }

    /**
     * 全部已读
     */
    public function allRead()
    {
        $user = $this->auth->getUser();
        if ($user->is_leader != 1) {
            $this->error('非法操作！');
        }

        $type = $this->request->post('type', 'platform');
        $res = $this->model
            ->where('user_id', $user->id)
            ->where('type', $type)
            ->setField('is_read', 1);

        if (!$res) {
            $this->error('操作失败！');
        }

        $this->success('操作成功！');
    }
}
